﻿using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Microsoft.IdentityModel.Tokens;
using TokenDemo.Web.Swagger;

namespace TokenDemo.Web.Jwt
{
    public class JwtTokenService : IJwtTokenService
    {
        private readonly JwtSettings _settings;
        public JwtTokenService(JwtSettings settings)
        {
            _settings = settings;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="claims"></param>
        /// <returns></returns>
        public string GenerateToken(Claim[] claims)
        {
            var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_settings.Key));
            var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

            var token = new JwtSecurityToken(_settings.Issuer,
                _settings.Audience,
                claims,
                expires: DateTime.UtcNow.Add(_settings.Expiration),
                signingCredentials: credentials,
                notBefore: DateTime.UtcNow);
            
            return new JwtSecurityTokenHandler().WriteToken(token);
        }
    }
}